﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Adapter.SISCONVE.Helper
{
    public static class Utilities
    {
        public static void DeleteFileDownload(string file)
        {
            string fileName = file + ConfigurationManager.AppSettings["FILE_DOWNLOAD_PREFIX"];
            string sourcePath = ConfigurationManager.AppSettings["DOWNLOAD_FOLDER"];

            if (System.IO.Directory.Exists(sourcePath))
            {
                string[] files = System.IO.Directory.GetFiles(sourcePath);

                // Copy the files and overwrite destination files if they already exist.
                foreach (string s in files)
                {
                    // Use static Path methods to extract only the file name from the path.
                    if (fileName == System.IO.Path.GetFileName(s))
                    {
                        System.IO.File.Delete(s);
                    }
                }
            }
        }

        public static string GetFullPathFile(string file)
        {
            string fullPathFile = string.Empty;
            string fileName = file;
            string sourcePath = ConfigurationManager.AppSettings["DOWNLOAD_FOLDER"];

            if (System.IO.Directory.Exists(sourcePath))
            {
                string[] files = System.IO.Directory.GetFiles(sourcePath);

                // Copy the files and overwrite destination files if they already exist.
                foreach (string s in files)
                {
                    // Use static Path methods to extract only the file name from the path.
                    if (System.IO.Path.GetFileName(s).Contains(fileName))
                    {
                        fullPathFile = System.IO.Path.GetFileName(s).Split('.')[0];
                    }
                }
            }
            return fullPathFile;
        }

        public static string GetConnectionString(string file)
        {
            Dictionary<string, string> props = new Dictionary<string, string>();

            // XLSX - Excel 2007, 2010, 2012, 2013
            props["Provider"] = "Microsoft.Jet.OLEDB.4.0;";
            props["Extended Properties"] = "Excel 8.0";
            props["Data Source"] = ConfigurationManager.AppSettings["DOWNLOAD_FOLDER"] + file + ConfigurationManager.AppSettings["FILE_DOWNLOAD_PREFIX"];

            StringBuilder sb = new StringBuilder();

            foreach (KeyValuePair<string, string> prop in props)
            {
                sb.Append(prop.Key);
                sb.Append('=');
                sb.Append(prop.Value);
                sb.Append(';');
            }

            return sb.ToString();
        }
    }
}
